<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use App\model\Author;

class A_AuthorController extends Controller
{
     /**
     * 管理员权限管理
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('admin.ll.author.a_author',['modules'=>self::getCates()]);
    }

     /**
     *处理分类
     */
    static public function getCates()
    {
        // $data = DB::select("select *,concat(path,',',id) as paths from cate order by paths");
        $data = DB::table('modules')->select(DB::raw("*,concat(m_path,',',id) as paths"))->orderBy('paths')->get();
        //获取该角色的权限
        $temp = DB::select('select m.* from modules as m,authors as a where a.role_id=2 and a.a_author=1 and a.module_id=m.id');
        
        foreach ($data as $key => $value) {
            $data[$key]['check']=0;
            // 统计一个字符串在另一个字符串出现次数
            $len = substr_count($value['m_path'],',');
           $data[$key]['m_name'] = str_repeat('|----',$len).$value['m_name'];
           foreach ($temp as $k => $v) {
               if($value['id']==$v['id']){
                    $data[$key]['check']=1;
               }
           }
        }
        return $data;
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $author = new Author;
        $data = $author::where('module_id',$id)->where('role_id',2)->first();
        //如果没有这条权限，则给角色设置权限
        if(!$data){
            $author->module_id=$id;
            $author->role_id=2;
            $author->a_author=1;
            $author->save();
        }else{
             if($data->a_author==0){
                $data->a_author=1;
             }else{
                $data->a_author=0;
             }
             $data->save();
        }
        return back();
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
